﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace visualizadordicom.Aux_Forms
{
    public partial class showhomogeneityimage : Form
    {
        bool selected;
        Rectangle StartPoint;

        public showhomogeneityimage(Bitmap bmp)
        {
            InitializeComponent();
            this.pictureBox1.Image = bmp;
            selected = false;
        }


        private void select(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left )
            {
                selected = true;
                StartPoint = new Rectangle(new Point(e.X, e.Y), new Size());
                pictureBox1.Refresh();
            }
        }

        private void updatearea(object sender, MouseEventArgs e)
        {
            // Update the actual size of the selection:
            if (selected)
            {

                StartPoint.Width = e.X - StartPoint.X;
                StartPoint.Height = e.Y - StartPoint.Y;

                // Redraw the picturebox:
                pictureBox1.Refresh();
            }
        }

        private void calculate(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                StartPoint.Width = (int)(StartPoint.Width * (double)this.pictureBox1.Image.Width / this.pictureBox1.Width);
                StartPoint.Height = (int)(StartPoint.Height * (double)this.pictureBox1.Image.Height / this.pictureBox1.Height);
                StartPoint.X = (int)(StartPoint.X * (double)this.pictureBox1.Image.Width / this.pictureBox1.Width);
                StartPoint.Y = (int)(StartPoint.Y * (double)this.pictureBox1.Image.Height / this.pictureBox1.Height);

                selected = false;

                int red = 0, white = 0;
                Bitmap bmp = (Bitmap)pictureBox1.Image;

                for (int i = StartPoint.X; i < StartPoint.X + StartPoint.Width; i++)
                    for (int j = StartPoint.Y; j < StartPoint.Y + StartPoint.Height; j++)
                    {
                        if (bmp.GetPixel(i, j).R > 0)
                            red++;
                        else
                            white++;
 
                    }

                double result = (double)red / (red + white);
                this.toolStripTextBox1.Text = Math.Round(100 * result, 2).ToString();
            }

        }

        private void paint(object sender, PaintEventArgs e)
        {
            if (selected)
            {
                // Draw a rectangle displaying the current selection
                Pen pen = Pens.Green;
                e.Graphics.DrawRectangle(pen, StartPoint);

            }
        }

        private void showoptions(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip1.Show();
            }

        }

        private void copiarImagemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetImage(pictureBox1.Image);
        }

    }
}
